package com.tongji.diy.model.furniturePlacementOptimization
{
	import com.tongji.diy.model.furniturePlacementOptimization.assembleComplement.NearBase;
	import com.tongji.diy.utils.OptimizationMath;
	
	import flash.geom.Vector3D;

	public class NearbyCollision
	{
		private var nearbyList:Vector.<NearBase>;
		private var data:DataAndFunction;
		private var exchangeP:Number = OptimizationParameter.EXCHANGE_PROBILITY;
		private var anginstWallList:Vector.<NearBase> = new Vector.<NearBase>;
		private var noAnginstWallList:Vector.<NearBase> = new Vector.<NearBase>;
		private var randNumList:Vector.<Number> = new Vector.<Number>;
		private var _maxStep:Number = -Infinity;
		public function NearbyCollision(nearbyList:Vector.<NearBase>, data:DataAndFunction)
		{
			this.nearbyList = nearbyList;
			this.data = data;
			buildAnginstWallList();
		}
		//构建靠墙列表与不靠墙列表
		private function buildAnginstWallList():void
		{
			for(var i:int = 0; i < nearbyList.length; ++i)
			{
				if(nearbyList[i].anganistWall)
					anginstWallList.push(nearbyList[i]);
				else
					noAnginstWallList.push(nearbyList[i]);
				//初始化随机数表
				randNumList.push(OptimizationMath.normalRandom(), OptimizationMath.normalRandom(), OptimizationMath.normalRandom());
			}
		}
		//agnistWall Swap
//		private function agnistWallSwap(nearbyA:NearBase, nearbyB:NearBase):Boolean
//		{
//			
//		}
		//noagnistWall Swap
		private function noAgnistWallSwap(nearbyA:NearBase, nearbyB:NearBase):void
		{
			var temp:Number =nearbyA.x;
			nearbyA.x = nearbyB.x;
			nearbyB.x = temp;
			temp= nearbyA.y;
			nearbyA.y = nearbyB.y;
			nearbyB.y = temp;
			temp= nearbyA.angle;
			nearbyA.angle = nearbyB.angle;
			nearbyB.y = temp;
		}
		//刷新randnumlist
		private function flushRandNumList():void
		{
			for(var i:int = 0; i < randNumList.length; ++i)
			{
				randNumList[i] = OptimizationMath.normalRandom();
			}
		}
		
		private function agnistWallArrange(step:Number,nearby:NearBase):void
		{
			//以下内容可能需要后期进行优化改动
			var first:Boolean = true;
			var tempRandNum:Number = data.reverseRandom(nearby);
			if(tempRandNum < 0)//说明nearby当前位置不在靠墙的位置
			{
				nearby.randNumX = OptimizationMath.randomNumNormalHandler(data.reverseRandom(data._furnitureBlock),step,_maxStep);
			}
			else
			{
				nearby.randNumX = OptimizationMath.randomNumNormalHandler(tempRandNum,step,_maxStep);
			}
			var r:Number = nearby.randNum.x;
			while(first || !data.boundingBoxInRoom(nearby.accessibleBox,data._room))
			{
				if(!first)
				{
					r = OptimizationMath.randomNumNormalHandler(nearby.randNum.x,step,_maxStep);
				}
				first = false;						
				var v:Vector3D = data.searchLocationWall(nearby,r);
				
				nearby.x = v.x;
				nearby.y = v.y;
				nearby.angle = v.z;
			}
		}
		private function noAgnistWallArrange(step:Number,nearby:NearBase):void
		{
			var first:Boolean = true;
			while(first || !data.boundingBoxInRoom(nearby.accessibleBox,data._room))
			{
				if(Math.random() < exchangeP && noAnginstWallList.length >=  2)
				{
					var sign:Boolean = true;
					var index:int = Math.random()*noAnginstWallList.length;
					while(noAnginstWallList[index] == nearby)
					{
						index = Math.random()*noAnginstWallList.length;
					}
					
					noAgnistWallSwap(nearby,noAnginstWallList[index]);							
					
				}
				else
				{
					if(!first)
					{
						nearby.flushRandNum();
					}
					first = false;
					nearby.x = nearby.recorder.x + step * nearby.randNum.x;/////没看懂
					nearby.y =  nearby.recorder.y + step * nearby.randNum.y;
					//nearbyList[i].angle = recoder[i].z + randNumList[3*i+2] * step / maxStep * Math.PI;//随机选择角度
					nearby.angle = data._orientedSelectionList[Math.floor(Math.random()*data._orientedSelectionList.length)];
				}
			}
			
		}
		
		public  function collpse_inRoom(step:Number, nearbyList:Vector.<NearBase>):void
		{
			OptimizationMath.assert(_maxStep != -Infinity);
			flushRandNumList();
			
			for(var  i:int = 0; i < nearbyList.length; ++i)
			{
				if(!nearbyList[i].anganistWall)
				{
					noAgnistWallArrange(step,nearbyList[i]);
				}
				else
				{
					agnistWallArrange(step,nearbyList[i]);
				}			
			}
			
		}
		public function set maxStep(maxStep:Number):void
		{
			this._maxStep = maxStep;
		}
		
	}
}